package com.bdqn.service;

import com.bdqn.mapper.SysUserMapper;
import com.bdqn.pojo.SysUser;
import com.bdqn.util.MyBatisUtil;
import com.bdqn.util.Page;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * SysUserServiceImpl
 *
 * @author LILIBO
 * @since 2022-09-08
 */
public class SysUserServiceImpl implements SysUserService {

    /**
     * 日志
     */
    private static final Logger logger = LogManager.getLogger(SysUserServiceImpl.class);

    /**
     * 添加系统用户
     *
     * @param sysUser 系统用户
     * @return
     */
    @Override
    public int save(SysUser sysUser) {
        if (sysUser == null) {
            return 0;
        }
        int add = 0;
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            add = sqlSession.getMapper(SysUserMapper.class).add(sysUser);
        } catch (Exception e) {
            logger.error(e.getMessage());
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return add;
    }

    /**
     * 账号密码登录接口
     *
     * @param account 账号
     * @param password 密码
     * @return 当前登录用户对象
     */
    @Override
    public SysUser login(String account, String password) {
        if (account == null || password == null) { // 参数错误
            logger.debug("参数错误");
            return null;
        }
        SysUser sysUser = null; // 如果登录成功返回用户信息

        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            sysUser = sqlSession.getMapper(SysUserMapper.class).getByAccount(account);
            if (sysUser == null) { // 账号不存在
                logger.debug("账号不存在");
                return null;
            }
            if (!password.equals(sysUser.getPassword())) { // 密码不匹配
                logger.debug("密码不匹配");
                return null;
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return sysUser;
    }

    /**
     * 根据真实姓名和角色ID查询用户总数
     *
     * @param queryRealName 真实姓名
     * @param queryRoleId 角色ID
     * @return
     */
    @Override
    public int count(String queryRealName, int queryRoleId) {
        int count = 0;
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(SysUserMapper.class).count(queryRealName, queryRoleId);
        } catch (Exception e) {
            logger.error(e.getMessage());
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }

    /**
     * 根据真实姓名和角色ID查询用户分页数据
     *
     * @param queryRealName 真实姓名
     * @param queryRoleId 角色ID
     * @param page 分页对象
     * @return
     */
    @Override
    public List<SysUser> queryPageByRealNameAndRoleId(String queryRealName, int queryRoleId, Page<SysUser> page) {
        List<SysUser> list = new ArrayList<>();
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            list = sqlSession.getMapper(SysUserMapper.class).queryPageByRealNameAndRoleId(queryRealName, queryRoleId, (page.getCurrPageNo() - 1) * page.getPageSize(), page.getPageSize());
        } catch (Exception e) {
            logger.error(e.getMessage());
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return list;
    }

}
